package net.oschina.simpleplus.aop;

import java.util.HashMap;
import java.util.Map;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ControllerInterceptor {
	private static final Logger logger = LoggerFactory.getLogger(ControllerInterceptor.class);

	@Pointcut("execution(* net.oschina.simpleplus.web..*(..)) && @annotation(org.springframework.web.bind.annotation.ResponseBody)")
	public void controllerMethodPointcut() {
	}

	@Around("controllerMethodPointcut()")
	public Object exec(ProceedingJoinPoint pjp) {
		Map<String, Object> result;
		try {
			result = (Map<String, Object>) pjp.proceed();
			result.put("status", 2);
			return result;
		} catch (Throwable e) {
			logger.error("",e);
			result = new HashMap<>();
			result.put("status", 400);
			result.put("errorMsg", e.getMessage());
			return result;
		}
	}
}
